package cn.jcet.module.bus.controller;


import cn.jcet.common.utils.DataGridView;
import cn.jcet.common.utils.ResultObj;
import cn.jcet.common.utils.WebUtils;
import cn.jcet.module.bus.domain.Goods;
import cn.jcet.module.bus.domain.Inport;
import cn.jcet.module.bus.domain.Provider;
import cn.jcet.module.bus.service.IGoodsService;
import cn.jcet.module.bus.service.IInportService;
import cn.jcet.module.bus.service.IProviderService;
import cn.jcet.module.bus.vo.InportVo;
import cn.jcet.module.sys.domain.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 鲜磊
 * @since 2019-12-25
 */
@RestController
@RequestMapping("/inport")
public class InportController {


    @Autowired
    private IInportService inportService;

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IProviderService providerService;

    /**
     * 加载全部商品进货数据
     * @param inportVo
     * @return
     */
    @GetMapping("loadAllInport")
    public DataGridView loadAllInport(InportVo inportVo){
        IPage<Inport> page = new Page<>(inportVo.getPage(),inportVo.getLimit());

        QueryWrapper<Inport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(inportVo.getProviderid()!=null && inportVo.getProviderid()!=0,"providerid",inportVo.getProviderid());
        queryWrapper.eq(inportVo.getGoodsid()!=null&&inportVo.getGoodsid()!=0,"goodsid",inportVo.getGoodsid());
        queryWrapper.ge(inportVo.getStartTime()!=null,"startTime",inportVo.getStartTime());
        queryWrapper.le(inportVo.getEndTime()!=null,"endTime",inportVo.getEndTime());
        queryWrapper.like(StringUtils.isNotBlank(inportVo.getOperateperson()),"operateperson",inportVo.getOperateperson());
        queryWrapper.like(StringUtils.isNotBlank(inportVo.getRemark()), "remark", inportVo.getRemark());
        queryWrapper.orderByDesc("inporttime");
        this.inportService.page(page,queryWrapper);

        List<Inport> records = page.getRecords();
        for (Inport inport : records){
            Provider provider = this.providerService.getById(inport.getProviderid());
            if(provider != null){
                inport.setProvidername(provider.getProvidername());
            }

            Goods goods = this.goodsService.getById(inport.getGoodsid());
            if(null != goods){
                inport.setGoodsname(goods.getGoodsname());
                inport.setSize(goods.getSize());
            }
        }
        return new DataGridView(page.getTotal(),records);
    }

    /**
     * 添加
     * @param inportVo
     * @return
     */
    @PostMapping("addInport")
    public ResultObj addInport(InportVo inportVo){
        try {
            inportVo.setInporttime(new Date());
            //获取当前用户名
            User user = (User) WebUtils.getSession().getAttribute("user");
            inportVo.setOperateperson(user.getName());
            this.inportService.save(inportVo);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_SUCCESS;
        }
    }

    /**
     * 修改
     * @param inportVo
     * @return
     */
    @PostMapping("updateInport")
    public ResultObj updateInport(InportVo inportVo){

        try {
            this.inportService.updateById(inportVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("deleteInport")
    public ResultObj deleteInport(Integer id){
        try {
            this.inportService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }



}
